<template>
  <div class="dbox">
    <div class="loginbox">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
        <el-form-item prop="username">
          <el-input prefix-icon="el-icon-user" v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input prefix-icon="el-icon-lock" v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">登陆</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  components: {},
  name: "",
  data() {
    return {
      ruleForm: {
        username: "",
        password: "",
      },
      //验证
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 3, max: 8, message: "长度在 3 到 8 个字符", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {},
  methods: {
    //登陆
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //获取接口数据
          this.$axios.post("login", this.ruleForm).then((res) => {
            console.log(res);
            //登陆成功  存个token  跳转页面 提示登录成功
            if (res.meta.status == 200) {
              sessionStorage.setItem("lc_04B", res.data.token);
              this.$router.push("/");
              this.$message({
                message: "登陆成功",
                type: "success",
              });
            }
          });
        } else {
          return false;
        }
      });
    },
    //重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
  computed: {},
  watch: {},
};
</script>

<style lang="scss" scoped>
.dbox {
  width: 100%;
  height: 100%;
  background-color: #4a5064;
  .loginbox {
    width: 460px;
    height: 340px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    box-shadow: white 0px 0px 30px;
    border-radius: 10px;
    background-color: white;
    .el-form {
      width: 80%;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);

      .el-form-item:nth-child(3) {
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>
